home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / textual / gnudiff / C / Dir < prev    next >
Text File  |  1991-09-16  |  6KB  |  204 lines

  1. /* Read, sort and compare two directories.  Used for GNU DIFF.
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "diff.h"
  21.  
  22. struct dirdata
  23. {
  24.   int length;            /* # elements in `files' */
  25.   char **files;            /* Sorted names of files in the dir */
  26. };
  27.  
  28. static struct dirdata dir_sort (char *, int);
  29. static int compare_names (const void *, const void *);
  30.  
  31. /* Read the directory named DIRNAME and return a sorted vector
  32.    of filenames for its contents.  NONEX nonzero means this directory is
  33.    known to be nonexistent, so return zero files.  */
  34.  
  35. static struct dirdata dir_sort (char *dirname, int nonex)
  36. {
  37.   register DIR *reading;
  38.   register struct direct *next;
  39.   struct dirdata dirdata;
  40.  
  41.   /* Address of block containing the files that are described.  */
  42.   char **files;
  43.  
  44.   /* Length of block that `files' points to, measured in files.  */
  45.   int nfiles;
  46.  
  47.   /* Index of first unused in `files'.  */
  48.   int files_index;
  49.  
  50.   if (nonex)
  51.     {
  52.       dirdata.length = 0;
  53.       dirdata.files = 0;
  54.       return dirdata;
  55.     }
  56.  
  57.   /* Open the directory and check for errors.  */
  58.   reading = opendir (dirname);
  59.   if (!reading)
  60.     {
  61.       fprintf (stderr, "%s: Cannot read directory %s\n", program, dirname);
  62.       dirdata.length = -1;
  63.       return dirdata;
  64.     }
  65.  
  66.   /* Initialize the table of filenames.  */
  67.  
  68.   nfiles = 100;
  69.   files = (char **) xmalloc (nfiles * sizeof (char *));
  70.   files_index = 0;
  71.  
  72.   /* Read the directory entries, and insert the subfiles
  73.      into the `files' table.  */
  74.  
  75.   while ((next = readdir (reading)) != 0)
  76.     {
  77.       if (files_index == nfiles)
  78.     {
  79.       nfiles *= 2;
  80.       files
  81.         = (char **) xrealloc (files, sizeof (char *) * nfiles);
  82.     }
  83.       files[files_index++] = concat (next->d_name, "", "");
  84.     }
  85.  
  86.   closedir (reading);
  87.  
  88.   /* Sort the table.  */
  89.   qsort (files, files_index, sizeof (char *), compare_names);
  90.  
  91.   /* Return a description of location and length of the table.  */
  92.   dirdata.files = files;
  93.   dirdata.length = files_index;
  94.  
  95.   return dirdata;
  96. }
  97.  
  98. /* Sort the files now in the table.  */
  99. static int compare_names (const void *ptr1, const void *ptr2)
  100. {
  101.   const char **file1 = (const char **)ptr1;
  102.   const char **file2 = (const char **)ptr2;
  103.   return strucmp (*file1, *file2);
  104. }
  105.  
  106. /* Compare the contents of two directories named NAME1 and NAME2.
  107.    This is a top-level routine; it does everything necessary for diff
  108.    on two directories.
  109.  
  110.    NONEX1 nonzero says directory NAME1 doesn't exist, but pretend it is
  111.    empty.  Likewise NONEX2.
  112.  
  113.    HANDLE_FILE is a caller-provided subroutine called to handle each file.
  114.    It gets five operands: dir and name (rel to original working dir) of file
  115.    in dir 1, dir and name pathname of file in dir 2, and the recursion depth.
  116.  
  117.    For a file that appears in only one of the dirs, one of the name-args
  118.    to HANDLE_FILE is zero.
  119.  
  120.    DEPTH is the current depth in recursion.
  121.  
  122.    Returns the maximum of all the values returned by HANDLE_FILE,
  123.    or 2 if trouble is encountered in opening files.  */
  124.  
  125. int diff_dirs (char *name1, char *name2, int (*handle_file)(), int depth, int nonex1, int nonex2)
  126. {
  127.   struct dirdata data1, data2;
  128.   register int i1, i2;
  129.   int val = 0;
  130.   int v1;
  131.  
  132.   /* Get sorted contents of both dirs.  */
  133.   data1 = dir_sort (name1, nonex1);
  134.   data2 = dir_sort (name2, nonex2);
  135.   if (data1.length == -1 || data2.length == -1)
  136.     {
  137.       if (data1.length >= 0)
  138.     free (data1.files);
  139.       if (data2.length >= 0)
  140.     free (data2.files);
  141.       return 2;
  142.     }
  143.  
  144.   i1 = 0;
  145.   i2 = 0;
  146.  
  147.   /* If -Sname was specified, and this is the topmost level of comparison,
  148.      ignore all file names less than the specified starting name.  */
  149.  
  150.   if (dir_start_file && depth == 0)
  151.     {
  152.       while (i1 < data1.length && strucmp (data1.files[i1], dir_start_file) < 0)
  153.     i1++;
  154.       while (i2 < data2.length && strucmp (data2.files[i2], dir_start_file) < 0)
  155.     i2++;
  156.     }
  157.  
  158.   /* Loop while files remain in one or both dirs.  */
  159.   while (i1 < data1.length || i2 < data2.length)
  160.     {
  161.       int nameorder;
  162.  
  163.       /* Compare next name in dir 1 with next name in dir 2.
  164.      At the end of a dir,
  165.      pretend the "next name" in that dir is very large.  */
  166.  
  167.       if (i1 == data1.length)
  168.     nameorder = 1;
  169.       else if (i2 == data2.length)
  170.     nameorder = -1;
  171.       else
  172.     nameorder = strucmp (data1.files[i1], data2.files[i2]);
  173.  
  174.       if (nameorder == 0)
  175.     {
  176.       /* We have found a file (or subdir) in common between both dirs.
  177.          Compare the two files.  */
  178.       v1 = (*handle_file) (name1, data1.files[i1], name2, data2.files[i2],
  179.                    depth + 1);
  180.       i1++, i2++;
  181.     }
  182.       if (nameorder < 0)
  183.     {
  184.       /* Next filename in dir 1 is less; that is a file in dir 1 only.  */
  185.       v1 = (*handle_file) (name1, data1.files[i1], name2, 0, depth + 1);
  186.       i1++;
  187.     }
  188.       if (nameorder > 0)
  189.     {
  190.       /* Next filename in dir 2 is less; that is a file in dir 2 only.  */
  191.       v1 = (*handle_file) (name1, 0, name2, data2.files[i2], depth + 1);
  192.       i2++;
  193.     }
  194.       if (v1 > val)
  195.     val = v1;
  196.     }
  197.   if (data1.files)
  198.     free (data1.files);
  199.   if (data2.files)
  200.     free (data2.files);
  201.  
  202.   return val;
  203. }
  204.